<?php
/**
 * 分类管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\cn\controllers;


use app\libs\IeltsController;
use app\libs\Method;
use app\libs\Sms;
use app\modules\cn\models\Info;
use app\modules\cn\models\ResourcesPhone;
use app\modules\content\models\Active;
use app\modules\content\models\Banner;
use app\modules\content\models\Classes;
use app\modules\content\models\Recommend;
use app\modules\content\models\Teachers;
use app\modules\user\models\Login;
use app\modules\cn\models\ParentUrl;
use app\modules\cn\models\ExamMessage;
use app\modules\content\models\Examroom;
use yii\data\Pagination;
use yii;
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';

if($origin==false)$origin='*';

header("Access-Control-Allow-Origin:$origin");
header('Access-Control-Allow-Headers: X-Requested-With');
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
header("Access-Control-Allow-Credentials: true");
class WapApiController extends IeltsController
{
    public $enableCsrfValidation = false;


    /**
     * 课程
     * @Author: Ferre
     * @create: 2021/3/29 16:55
     */
    public function actionNewCourse()
    {
        $type     = Yii::$app->request->post('type');    //1/6.0 2/6.5 3/7.0
        $page     = Yii::$app->request->post('page',1);
        $pageSize = Yii::$app->request->post('pageSize',15);
        $key      = Yii::$app->request->post('key');
        if (!empty($key)){
            $where = "name like '%$key%'";
        }else{
            $where = "1=1";
        }
        if (!empty($type)){
            if ($type == 1) $real_type = 3;
            if ($type == 2) $real_type = 4;
            if ($type == 3) $real_type = 5;
            $where .= " and score=$real_type";
        }
        $count  = Classes::find()->asArray()->where($where)->count();
        $course = Classes::find()->asArray()->where($where)->offset(($page - 1) * $pageSize)->limit($pageSize)->all();
        $banner = Banner::find()->asArray()->where(['cate' => 1])->orderBy('id desc')->all();
        foreach ($banner as $k => $v){
            $banner[$k]['image'] = $v['pic'];
            $banner[$k]['title'] = $v['name'];
        }
        foreach ($course as $k => $v){
            $course[$k]['image']  = $v['pic'];
            $course[$k]['cnName'] = $v['duration'];
        }
        $course_data = ['course' => $course, 'count' => $count, 'countPage' => ceil(count($course)/$pageSize)];
        die(json_encode(['code' => '1', 'message' => '成功', 'data' => ['course' => $course_data, 'banner' => $banner]]));
    }

    /**
     * 新首页数据
     * @Author: Ferre
     * @create: 2021/3/29 17:24
     */
    public function actionNewIndexData()
    {
        $news = Info::find()->asArray()->where(['cate' => 1])->orderBy('viewCount desc')->limit(2)->all();
        foreach ($news as $k => $v){
            $news[$k]['title'] = $v['name'];
            $news[$k]['image'] = $v['pic'];
            $news[$k]['createTime'] = date('Y-m-d H:i:s', $v['createTime']);
        }
        $banner = Banner::find()->asArray()->where(['cate' => 1, 'type'=>2])->orderBy('id desc')->limit(5)->all();
        $mid = Banner::find()->asArray()->where(['cate' => 2])->orderBy('id desc')->one();
        $bot = Banner::find()->asArray()->where(['cate' => 3])->orderBy('id desc')->one();
        $lead_news = Info::find()->asArray()->where(['cate' => 3])->orderBy('sort desc')->limit(2)->all();
        foreach ($banner as $k => $v){
            $banner[$k]['image'] = $v['pic'];
            $banner[$k]['title'] = $v['name'];
            if ($v['jumpType'] == 1) {  // banner跳转类型jumpType为资讯时 需要查询资讯的分类id
                $tmp = Info::find()->where(['id'=>$v['contentId']])->asArray()->one();
                $banner[$k]['infoCateId'] = $tmp?$tmp['cate']:1;
            } else {
                $banner[$k]['infoCateId'] = null;
            }
        }
        // 推荐课程 只展示2个 APP推荐课程数据
        $recommend = Classes::find()->where(['classesPort'=>2,'showType'=>1])->orderBy('id desc')->limit(2)->asArray()->all();
        // 雅思资讯 最新的两条
        $time = time();
        $ielts_info = Info::find()->asArray()->where("(pushTime<=$time or pushTime is null) and cate=1")->orderBy('id desc')->limit(2)->all();
        // 雅思活动 最新的两条
        $active = Active::find()->orderBy('id desc')->limit(2)->asArray()->all();
        $active = Active::getReservation($active);
        // 底部友情链接
        $footer_url = ParentUrl::find()->where(['type'=>4])->orderBy('sort asc')->asArray()->all();
        $handle = fopen('default_value.txt', "r");//读取二进制文件时，需要将第二个参数设置成'rb'
        //通过filesize获得文件大小，将整个文件一下子读到一个字符串中
        $contents = fread($handle, filesize('default_value.txt'));
        fclose($handle);
        $contents = json_decode($contents, TRUE);
        die(json_encode(['code' => 1, 'data' => ['news' => $news, 'banner' => $banner, 'mid_banner' => $mid, 'bot_banner' => $bot, 'class_num' => $contents['wap_class_num'], 'lead_news' => $lead_news, 'recommend'=>$recommend, 'ielts_info'=>$ielts_info, 'active'=>$active, 'footer_url'=>$footer_url]]));
    }

    /**
     * 获取资料的手机号码收集
     * @Author: Ferre
     * @create: 2021/3/15 14:27
     */
    public function actionGetResources()
    {
        $posts = Yii::$app->request->post();
        ResourcesPhone::savePhone($posts);  //保存电话
        die(json_encode(['code' => 1, 'message' => '成功']));
    }

    /**
     * 机经
     * @Author: Ferre
     * @create: 2021/3/30 10:56
     */
    public function actionGetMaterial()
    {
        $banner = Banner::find()->asArray()->where(['cate' => 1])->orderBy('id desc')->all();
        foreach ($banner as $k => $v){
            $banner[$k]['image'] = $v['pic'];
        }
        $data['data'] = [
            ['month' => '2020-01', 'title' => '2020年1月雅思考试机经'],
            ['month' => '2020-02', 'title' => '2020年2月雅思考试机经'],
            ['month' => '2020-03', 'title' => '2020年3月雅思考试机经'],
            ['month' => '2020-04', 'title' => '2020年4月雅思考试机经'],
            ['month' => '2020-05', 'title' => '2020年5月雅思考试机经'],
            ['month' => '2020-06', 'title' => '2020年6月雅思考试机经'],
            ['month' => '2020-07', 'title' => '2020年7月雅思考试机经'],
            ['month' => '2020-08', 'title' => '2020年8月雅思考试机经'],
            ['month' => '2020-09', 'title' => '2020年9月雅思考试机经'],
            ['month' => '2020-10', 'title' => '2020年10月雅思考试机经'],
            ['month' => '2020-11', 'title' => '2020年11月雅思考试机经'],
            ['month' => '2020-12', 'title' => '2020年12月雅思考试机经'],
            ['month' => '2021-01', 'title' => '2021年01月雅思考试机经'],
            ['month' => '2021-02', 'title' => '2021年02月雅思考试机经'],
            ['month' => '2021-03', 'title' => '2021年03月雅思考试机经'],
        ];
        $data['totalPage'] = 1;
        $data['currentPage'] = 1;
        $data['count'] = 15;
        die(json_encode(['code' => 1, 'data' => ['exam' => $data, 'banner' => $banner]]));
    }

    /**
     * 课程搜索
     * @Author: Ferre
     * @create: 2021/3/30 11:00
     */
    public function actionGetCourseList()
    {
        $new = [];
        $key   = Yii::$app->request->post('key');
        $where = "name like '%$key%'";
        $course_data = Classes::find()->asArray()->where($where)->all();
        foreach ($course_data as $k => $v){
            $new[$k]['id'] = $v['id'];
            $new[$k]['name'] = $v['name'];
        }
        die(json_encode(['code' => '1', 'message' => '成功', 'data' => $new]));
    }

    public function actionCourseDetail()
    {
        $id = Yii::$app->request->post('id');
        $data = Classes::find()->asArray()->where(['id' => $id])->one();
        die(json_encode(['code' => '1', 'message' => '成功', 'data' => $data]));
    }

    /**
     * 处理领取值
     * @Author: Ferre
     * @create: 2021/3/30 13:42
     */
    public function actionHandleSign()
    {
        $handle = fopen('default_value.txt', "r");//读取二进制文件时，需要将第二个参数设置成'rb'
        //通过filesize获得文件大小，将整个文件一下子读到一个字符串中
        $contents = fread($handle, filesize('default_value.txt'));
        fclose($handle);
        $handle = fopen('default_value.txt', "w");
        $contents = json_decode($contents, TRUE);
        if ($contents['wap_class_num'] == 1){
            $num = 25;
        }else{
            $num = $contents['wap_class_num'] - 1;
        }
        $contents['wap_class_num'] = $num;
        fwrite($handle, json_encode($contents));
        fclose($handle);
        die(json_encode(['code' => '1', 'message' => '成功', 'data' => $num]));
    }

    /**
     * 资讯列表
     * @Author: yoyo
     * @create: 2021/3/31 11:00
     */
    public function actionInfoList()
    {
        $page = \Yii::$app->request->get('page', 1);
        $pageSize = \Yii::$app->request->get('pageSize', 10);
        $cate = \Yii::$app->request->get('cate', 1);
        $offset = $pageSize * ($page - 1);
        $time=time();
        $data = Info::find()->asArray()->where("cate=$cate and (pushTime<=$time or pushTime is null)")->offset($offset)->limit($pageSize)->orderBy('id desc')->all();
        $count = count(Info::find()->asArray()->select('id')->where("cate=$cate and (pushTime<=$time or pushTime is null)")->all());
        $countPage = $count!=false?ceil($count/$pageSize):0;
        $banner = Banner::find()->asArray()->limit(4)->where('cate=4')->orderBy('sort asc,viewCount desc')->all();
        $focus= Recommend::find()->asArray()->where('type=1')->orderBy('sort asc,id desc')->limit(12)->all();
        die(json_encode(['code' => '1', 'message' => '成功', 'banner' => $banner,'data'=>$data ,'countPage'=>$countPage,'focus'=>$focus]));

    }

    /**
     * 资讯详情
     * @Author: yoyo
     * @create: 2021/3/31 11:00
     */
    public function actionInfoDetail()
    {
        $id = \Yii::$app->request->get('id');
        $detail = Info::find()->asArray()->where('id='.$id)->one();
        if($detail!=false){
            $time=time();
            Info::updateAll(['viewCount' => ($detail['viewCount'] + 1)], "id=$id");
            $detail['content'] = str_replace("/files/", "http://www.thinkuprep.com/files/", $detail['content']);
            $recommend = Info::find()->asArray()->where('cate='.$detail['cate'].' and pushTime<='.$time)->orderBy('viewCount desc')->limit(4)->all();
            $course = Classes::find()->asArray()->orderBy('id desc')->one();
            $focus= Recommend::find()->asArray()->where('type=1')->orderBy('sort asc,id desc')->limit(12)->all();
            // 资讯详情头像
            $detail['autherImage'] = 'https://file.viplgw.cn/ui/thinku-ielts-new/images/zx/13@2x.png';
            die(json_encode(['code' => '1', 'message' => '成功','course'=>$course, 'recommend' => $recommend,'data'=>$detail,'focus'=>$focus]));
        }
        die(json_encode(['code' => 0, 'message' => '请求错误']));

    }

    /**
     * 活动列表
     * @Author: yoyo
     * @create: 2021/3/31 11:00
     */
    public function actionActiveList()
    {
        $page = \Yii::$app->request->get('page',1);
        $pageSize = \Yii::$app->request->get('pageSize',10);
        $offset=$pageSize*($page-1);
        $active=Active::find()->asArray()->offset($offset)->limit($pageSize)->orderBy('id desc')->all();//全部 有分页
        $active= Active::getReservation($active);
        $count = count(Active::find()->select('id')->asArray()->all());
        $countPage = $count!=false?ceil($count/$pageSize):0;
        $focus= Recommend::find()->asArray()->where('type=1')->orderBy('sort asc,id desc')->limit(12)->all();
        die(json_encode(['code' => '1', 'message' => '成功', 'active' => $active,'countPage'=>$countPage,'focus'=>$focus]));

    }

    /**
     * 活动详情
     * @Author: yoyo
     * @create: 2021/3/31 11:00
     */
    public function actionActiveDetail()
    {
        $id = \Yii::$app->request->get('id');
        $active=Active::find()->asArray()->where('id='.$id)->one();
        $recommend = Active::find()->asArray()->orderBy('id desc')->limit(4)->all();
        $course = Classes::find()->asArray()->orderBy('id desc')->one();
        $active['introduction'] = str_replace("/files/", "http://www.thinkuprep.com/files/",$active['introduction']);
        $focus= Recommend::find()->asArray()->where('type=1')->orderBy('sort asc,id desc')->limit(12)->all();
        die(json_encode(['code' => '1', 'message' => '成功', 'course'=>$course,'active' => $active,'recommend'=>$recommend,'focus'=>$focus]));

    }

    /**
     * 短信接口
     * @sjeam
     */
    public function actionPhoneCode()
    {
        $session = Yii::$app->session;
        $sms = new Sms();
        $phoneNum = Yii::$app->request->post('phone');
        if (!empty($phoneNum)) {
            $phoneCode = mt_rand(100000, 999999);
            $session->set($phoneNum . 'phoneCode', $phoneCode);
            $session->set('phoneTime', time());
            $content = '申友网验证码：' . $phoneCode . '（10分钟有效），您正在通过手机号进行验证';
            $sms->send($phoneNum, $content, $ext = '', $stime = '', $rrid = '');
            $res['code'] = 1;
            $res['message'] = '短信发送成功！';
            // $res['phoneCode'] = $phoneCode;
            $res['str'] = base64_encode($phoneCode);
        } else {
            $res['code'] = 0;
            $res['message'] = '发送失败!手机号码为空！';
        }
        die(json_encode($res));
    }

    /**
     * 客户留言
     * @return string
     * @yoyo
     */
    public function actionLeaveWords()
    {
        $phone = Yii::$app->request->post('phone');
        $code = Yii::$app->request->post('code');
        $str = Yii::$app->request->post('str');
        $remark = Yii::$app->request->post('remark');
        $city = Yii::$app->request->post('city','');
        if ($code != false && $str != false) {
            if(base64_encode($code) != $str){
                die(json_encode(['code' => 0, 'message' => '验证码错误']));
            }
        }
        $download = new ResourcesPhone();
        $download->phone = $phone;
        $download->type = 1;
        $download->createTime = time();
        $download->remark = $remark;
        $download->city = $city;
        $download->save();
        $res['code'] = 1;
        $res['message'] = '我们的工作人员将于1-2个工作日内跟你联系';
        die(json_encode($res));
    }

    /**
     * 总调度
     * @Obelisk
     * 登录
     */
    public function actionUnifyLogin()
    {
        $session = Yii::$app->session;
        $logins = new Login();
        $uid = Yii::$app->request->get('uid');
        $username = Yii::$app->request->get('username');
        $phone = Yii::$app->request->get('phone');
        $password = Yii::$app->request->get('password');
        $email = Yii::$app->request->get('email');
        $loginsdata = $logins->find()->where("uid=$uid")->one();
        if (empty($loginsdata['id'])) {
            $where = '';
            if (!empty($email)) {
                $where .= empty($where) ? "email='$email'" : " or email='$email'";
            }
            if (!empty($username)) {
                $where .= empty($where) ? "userName='$username'" : " or userName='$username'";
            }
            if (!empty($phone)) {
                $where .= empty($where) ? "phone='$phone'" : " or phone='$phone'";
            }
            $loginsdata = $logins->find()->where("$where")->one();
            if (empty($loginsdata['id'])) {
                $login = new Login();
                $login->phone = $phone;
                $login->userPass = $password;
                $login->email = $email;
                $login->createTime = time();
                $login->userName = $username;
                $login->uid = $uid;
                $login->save();
                $loginsdata = $logins->find()->where("$where")->one();
            } else {
                if ($phone != $loginsdata['phone']) {
                    Login::updateAll(['phone' => $phone], "id={$loginsdata['id']}");
                }
                if ($email != $loginsdata['email']) {
                    Login::updateAll(['email' => "$email"], "id={$loginsdata['id']}");
                }
                if ($username != $loginsdata['userName']) {
                    Login::updateAll(['userName' => "$username"], "id={$loginsdata['id']}");
                }
                if ($uid != $loginsdata['uid']) {
                    Login::updateAll(['uid' => "$uid"], "id={$loginsdata['id']}");
                }
                $loginsdata = $logins->find()->where("id={$loginsdata['id']}")->one();
            }
        } else {
            if ($phone != $loginsdata['phone']) {
                Login::updateAll(['phone' => $phone], "id={$loginsdata['id']}");
            }
            if ($email != $loginsdata['email']) {
                Login::updateAll(['email' => "$email"], "id={$loginsdata['id']}");
            }
            if ($username != $loginsdata['userName']) {
                Login::updateAll(['userName' => "$username"], "id={$loginsdata['id']}");
            }
            $loginsdata = $logins->find()->where("id={$loginsdata['id']}")->one();
        }
        $session->set('userId', $loginsdata['id']);
        $session->set('userData', $loginsdata);
        $session->set('uid', $loginsdata['uid']);
        if($loginsdata['image']==false){
            $loginsdata['image']='https://www.thinkwithu.com/cn/images/indexNew/icon-defaultOne.png';
        }else{
            $loginsdata['image']='https://www.thinkwithu.com'.$loginsdata['image'];
        }
        die(json_encode(['userId' => $loginsdata['id'], 'sid' => session_id(),'code'=>1,'image'=>$loginsdata['image']]));
//        echo $callback . '(' . json_encode(['userId' => $loginsdata['id'], 'sid' => session_id()]) . ')';
//        exit;
    }

    public function actionLoginOut()
    {
        $session = Yii::$app->session;
        $time = time();
        setcookie('sslToken', 'out', ($time - 3600), '/');
        setcookie('sslTokenOut', 'out', ($time + 86400 * 60), '/');
        $session->remove('userData');
        $session->remove('userId');
        $session->remove('uid');
        session_destroy();
//        $loginOut = uc_user_synlogout();
        $data = ['code' => 1, 'message' => '退出成功!'];
        die(json_encode($data));
    }

    /**
     * [actionCourseList 课程列表-推荐课程]
     * @Author don
     * @Date   2021-11-09
     * @return [type]     [description]
     */
    public function actionCourseList()
    {
        $page = Yii::$app->request->post('page', 1);
        $pageSize = Yii::$app->request->post('pageSize', 15);
        $offset = $pageSize * ($page - 1);
        $data = Classes::find()->where(['showType'=>1,'classesPort'=>2])->offset($offset)->limit($pageSize)->orderBy('id desc')->asArray()->all();
        $count = Classes::find()->where(['showType'=>1,'classesPort'=>2])->count();
        $totalPage = ceil($count / $pageSize);

        die(json_encode(['code'=>1, 'data'=>$data, 'page'=>$page, 'count'=>$count, 'totalPage'=>$totalPage]));
    }

    /**
     * [actionWapCourseDetail 课程详情]
     * @Author don
     * @Date   2021-11-09
     * @return [type]     [description]
     */
    public function actionWapCourseDetail()
    {
        $id = Yii::$app->request->post('id');
        $data = Classes::find()->where('id='.$id)->asArray()->one();
        // 标签
        $data['course_tag'] = $data['course_tag']?explode(',', $data['course_tag']):null;
        // 详情内容
        $service = !empty($data['service'])?explode(PHP_EOL,$data['service']):null;
        $structure = !empty($data['structure'])?explode(PHP_EOL,$data['structure']):null;
        $data['serviceTitle'] = $data['structureTitle'] = '';
        $data['serviceContent'] = $data['structureContent'] = null;
        if ($service) {
            $data['serviceTitle'] = array_shift($service);
            $data['serviceContent'] = $service;
        }
        if ($structure) {
            $data['structureTitle'] = array_shift($structure);
            $data['structureContent'] = $structure;
        }
        $data['course_description1'] = 'https://www.thinkuprep.com' . $data['course_description1'];
        // // 详情老师信息
        // if ($data['systemTeacher']) {
        //     $str = $data['systemTeacher'];
        //     $data['systemTeacher'] = Teachers::find()->where("id in ($str)")->asArray()->all();
        // } else {
        //     $data['systemTeacher']=[];
        // }
        // $model = new Teachers();
        // $data['teachers'] = $model->getTeachers($data['teacher']);
        // unset($data['teacher']);

        // 更新浏览量
        Classes::updateAll(['viewCount' => ($data['viewCount'] + 1)], "id=$id");

        die(json_encode(['code'=>1, 'data'=>$data]));
    }


    /**
     * [actionIeltsInfo 考点列表]
     * @Author don
     * @Date   2021-10-26
     * @return [type]     [description]
     */
    public function actionExamList()
    {
        $area = Yii::$app->request->post('area', 0); // 0全部 1华东2华南3华中4华北5西北6西南7东北
        $data = Examroom::getExamroomByArea($area);

        if (empty($data)) {
            die(json_encode(['code'=>0, 'message'=>'未找到区域考点信息！']));
        }

        die(json_encode(['code'=>1, 'data'=>$data]));
    }
    
    /**
     * [actionIeltsInfoDetail 考点详情]
     * @Author don
     * @Date   2021-10-26
     * @return [type]     [description]
     */
    public function actionExamDetail()
    {
        $city = Yii::$app->request->post('city', '上海');
        $data = Examroom::getExamroomByCity($city);
        if (empty($data)) {
            die(json_encode(['code'=>0, 'message'=>'未找该区域考点详情！']));
        }
        die(json_encode(['code'=>1, 'data'=>$data]));
    }

    /**
     * [actionSendExamroom 发送考点信息]
     * @Author don
     * @Date   2021-10-28
     * @return [type]     [description]
     */
    public function actionSendExamMessage()
    {
        $sms = new Sms();
        $phone = Yii::$app->request->post('phone');
        $code = Yii::$app->request->post('code');
        $str = Yii::$app->request->post('str');
        $id = Yii::$app->request->post('id');//考位的id
        $uid = Yii::$app->request->post('uid');

        if ($code != false && $str != false) {
            if(base64_encode($code) != $str){
                die(json_encode(['code' => 0, 'message' => '验证码错误']));
            }
        }

        if (!empty($phone)) {
            // $data = json_decode(Method::post('https://www.thinkuprep.com/cn/api/check-message', ['uid' => $uid, 'id' => $id]), true);
            // 查询发送记录 单个考场一个用户只能发送两次并且满足间隔30分钟
            $count = ExamMessage::find()->where(['uid'=>$uid, 'examId'=>$id])->count();
            if ($count >= 2) {
                $res['code'] = 0;
                $res['message'] = '用户每个考点信息只能发送两次！';
                die(json_encode($res));
            }
            if ($count == 1) {  // 已经发送一条，再发送第二条的时候需要间隔30分钟
                $examMessage = ExamMessage::find()->where(['uid'=>$uid, 'examId'=>$id])->asArray()->one();
                if (time()-$examMessage['createTime'] < 1800) {
                    $res['code'] = 0;
                    $res['message'] = '考点短信发送间隔需要30分钟！';
                    die(json_encode($res));
                }
            }
            // 存考点短信发送记录
            $model = new ExamMessage();
            $model->examId = $id;
            $model->uid = $uid;
            $model->createTime = time();
            if (!$model->save()) {
                $res['code'] = 0;
                $res['message'] = '考点短信发送记录保存失败！';
                die(json_encode($res));
            }

            $data = Examroom::find()->asArray()->where("id=$id")->one();//考点信息

            if (!$data) {
                die(json_encode(['code'=>0, 'message'=>'无此考点信息！']));
            }

            $content = '您好，欢迎使用申友雅思' . $data['city'] . '地区考点信息查询，以下是您所查询的内容：
考点代码：' . $data['code'] . '，
考点名称：' . $data['name'] . '，
地址：' . $data['address'] . '，
笔试考试：' . $data['writtenExam'] . '，
口试考场：' . $data['spokenExam'] . '，
电话：' . $data['phone'] . '，
交通：' . $data['traffic'] . '。
感谢您的使用，如有更多疑问，可拨打申友雅思全国免费咨询热线：400-600-1123
';
            $smsres = $sms->send($phone, $content, $ext = '', $stime = '', $rrid = '');
            $res['code'] = 1;
            $res['message'] = '短信发送成功！';
        } else {
            $res['code'] = 0;
            $res['message'] = '发送失败!手机号码为空！';
        }
        die(json_encode($res));
           

    }   


    /**
     * [actionTeacherList 师资团队列表]
     * @Author don
     * @Date   2021-10-26
     * @return [type]     [description]
     */
    public function actionTeacherList()
    {
        $page = Yii::$app->request->post('page', 1); // 页码
        $seniority = Yii::$app->request->post('seniority', 0); // 讲师级别 0全部 1首席 2总监 3金牌
        $subject = Yii::$app->request->post('subject', 0); // 主讲科目 0全部 听力 口语 阅读 写作
        $pageSize = 10;
        $data = Teachers::getWapTeacher($seniority, $subject, $page, $pageSize);

        $seniorityArr = array(
            1 => '首席讲师',
            2 => '总监讲师',
            3 => '金牌讲师',
        );
        if ($data['data']) {
            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['image'] = "https://www.thinkuprep.com".$value['pic'];
                $data['data'][$key]['seniority'] = isset($seniorityArr[$value['seniority']]) ? $seniorityArr[$value['seniority']] : '';
                $data['data'][$key]['subject'] = $value['subject'] ? explode(' ', $value['subject']) : '';
            }
        }

        die(json_encode(['code'=>1, 'data'=>$data]));
    }

    /**
     * [actionTeacherDetail 老师详情]
     * @Author don
     * @Date   2021-10-26
     * @return [type]     [description]
     */
    public function actionTeacherDetail()
    {
        $id = Yii::$app->request->post('id');
        $data = Teachers::find()->asArray()->where('id='.$id)->one();
        $seniorityArr = array(
            1 => '首席讲师',
            2 => '总监讲师',
            3 => '金牌讲师',
        );
        $data['seniority'] = isset($seniorityArr[$data['seniority']]) ? $seniorityArr[$data['seniority']] : '';
        $data['image'] = "https://www.thinkuprep.com".$data['pic'];

        die(json_encode(['code'=>1, 'data'=>$data]));
    }

    /**
     * [actionActivityList 活动列表]
     * @Author don
     * @Date   2021-11-10
     * @return [type]     [description]
     */
    public function actionActivityList()
    {
        $page = Yii::$app->request->post('page',1);
        $pageSize = Yii::$app->request->post('pageSize',10);
        $type = Yii::$app->request->post('type', 0); // 0 全部 1进行中 2未开始
        $offset = $pageSize * ($page - 1);
        $today = date('Y-m-d H:i:s',time());
        if ($type == 0) {
            $data = Active::find()->offset($offset)->limit($pageSize)->orderBy('id desc')->asArray()->all();
            $count = Active::find()->select('id')->count();
        } elseif ($type == 1) {
            $data = Active::find()->where('startTime<="'.$today.'" and endTime>="'.$today.'"')->offset($offset)->limit($pageSize)->orderBy('id desc')->asArray()->all();
            $count = Active::find()->select('id')->where('startTime<="'.$today.'" and endTime>="'.$today.'"')->count();
        } elseif ($type == 2) {
            $data = Active::find()->where('startTime>"'.$today.'"')->offset($offset)->limit($pageSize)->orderBy('id desc')->asArray()->all();
            $count = Active::find()->select('id')->where('startTime>"'.$today.'"')->count();
        }
        if ($data) {
            $data = Active::getReservation($data);
        }

        $totalPage = $count!=false ? ceil($count / $pageSize) : 0;
        
        die(json_encode(['code'=>1, 'data'=>$data, 'page'=>$page, 'count'=>$count, 'totalPage'=>$totalPage]));
    }

    public function actionAds()
    {
        return $this->renderPartial('index');
    }

    /**
     * [actionAcitvityDetail 活动详情]
     * @Author don
     * @Date   2021-11-10
     * @return [type]     [description]
     */
    public function actionActivityDetail()
    {
        $id = Yii::$app->request->post('id');
        $data = Active::find()->where('id='.$id)->asArray()->one();
        // 资讯详情头像
        $data['autherImage'] = 'https://file.viplgw.cn/ui/thinku-ielts-new/images/zx/13@2x.png';
        if(!strstr($data['introduction'], 'https://www.thinkuprep.com')){
            $data['introduction'] = str_replace("/files/", "https://www.thinkuprep.com/files/",$data['introduction']);
        }
        $time = time();
        $recommend = Info::find()->asArray()->where('pushTime<='.$time)->orderBy('createTime desc')->limit(4)->all();

        die(json_encode(['code'=>1, 'data'=>$data, 'recommend'=>$recommend]));

    }

    // /**
    //  * [actionCheckUser 关于我们 查看用户是否登录]
    //  * @Author don
    //  * @Date   2021-11-09
    //  * @return [type]     [description]
    //  */
    // public function actionIsLogin()
    // {
    //     $uid = Yii::$app->session->get('uid');
    //     if (!$uid) {
    //         die(json_encode(['code'=>5, 'message'=>'用户未登录！']));
    //     }
    //     die(json_encode(['code'=>1, 'message'=>'已登录']));
    // }

    /**
     * [actionCheckUserReserve 关于我们 查看用户是否领取（领取试听课程是否展示）]
     * @Author don
     * @Date   2021-11-09
     * @return [type]     [description]
     */
    public function actionCheckUserReserve()
    {
        $uid = Yii::$app->request->post('uid');
        $remark = '关于我们-领取试听课程';
        $data = ResourcesPhone::find()->where(['uid'=>$uid, 'remark'=>$remark, 'type'=>1])->asArray()->one();
        if (!empty($data)) {
            die(json_encode(['code'=>0, 'message'=>'已经预约过wap试听课了！']));
        }
        die(json_encode(['code'=>1, 'message'=>'还未预约wap试听课']));
    }

    public function actionAmanda()
    {
        $page = \Yii::$app->request->get('page', 1);
        $pageSize = \Yii::$app->request->get('pageSize', 8);
        $offset = $pageSize * ($page - 1);
        $time = time();
        $data = Info::find()->asArray()->where("(pushTime<=$time or pushTime is null) and cate=6" )->offset($offset)->limit($pageSize)->orderBy('id desc')->all();
        $count = count(Info::find()->asArray()->where("(pushTime<=$time or pushTime is null) and cate=6" )->all());
        $course = Classes::find()->asArray()->limit(2)->orderBy('viewCount desc')->all();
        $top = Info::find()->asArray()->where('cate=3')->limit(4)->orderBy('viewCount desc')->all();
        $banner = Banner::find()->asArray()->where('cate=4')->limit(4)->orderBy('sort asc,viewCount desc')->all();
        $countPage = $count != false ? ceil($count / $pageSize) : 0;
        $pageArray = Method::page($countPage, $page);
        die(json_encode(['code'=>1, 'data'=>['banner' => $banner, 'data' => $data, 'pageArray' => $pageArray, 'course' => $course, 'top' => $top, 'pageSize' => $pageSize]]));
    }
}
